package com.example.apione.controller;

import com.example.apione.dao.ApiInfoDao;
import com.example.apione.dao.WebInfoDao;
import com.example.apione.entity.WebInfo;
import com.example.apione.model.getApimodel;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 尘落
 * @date 2023/2/23
 */
@Data
@Controller
public class AllMap {
    private final ApiInfoDao apiInfoDao;
    private final WebInfoDao webInfoDao;
    @RequestMapping("/")
    public String index(Model model ,HttpServletRequest request){
        WebInfo titledata=webInfoDao.selectById(1);
        String title=titledata.getTitle();
        String keywords=titledata.getKeywords();
        String desc=titledata.getDesc();
        model.addAttribute("title",title);
        model.addAttribute("keywords",keywords);
        model.addAttribute("desc",desc);
        return "index/index";
    }
    @RequestMapping("/doc/**")
    public String doc(Model model, HttpServletRequest request){
        try {
            WebInfo titledata=webInfoDao.selectById(1);
            String title=titledata.getTitle();
            String keywords=titledata.getKeywords();
            String desc=titledata.getDesc();
            model.addAttribute("title",title);
            model.addAttribute("keywords",keywords);
            model.addAttribute("desc",desc);
            String url=request.getServerName();
            String type= request.getServletPath().substring(5);
            String port= String.valueOf(request.getServerPort());
            String example= getApimodel.onedoc(apiInfoDao,type);
            if (port.equals("80")){
                model.addAttribute("url",url);
            }else {
                model.addAttribute("url",url+":"+port);
            }
            model.addAttribute("type",type);
            model.addAttribute("example",example);
            return "index/doc";
        }catch (Exception e){
            return "404";
        }

    }
    @RequestMapping("/admin")
    public String admin(){
        return "admin/index";
    }

    @RequestMapping("/login")
    public String login(){
        return "index/login";
    }

    @RequestMapping("/admin/addapi")
    public String addapi(){
        return "admin/addapi";
    }

    @RequestMapping("/admin/editapi")
    public String editapi(@RequestParam(value = "doc") String doc ,Model model){
        model.addAttribute("doc",doc);
        return "admin/editapi";
    }

    @RequestMapping("/admin/controlapi")
    public String controlapi(){
        return "admin/controlapi";
    }

    @RequestMapping("/admin/websetting")
    public String websetting(){
        return "admin/websetting";
    }

    @RequestMapping("/admin/log")
    public String log(){
        return "admin/access_log";
    }
    @RequestMapping("/admin/loginlog")
    public String loginlog(){
        return "admin/admin_log";
    }

    @RequestMapping("/admin/password")
    public String passwd(){
        return "admin/password";
    }
}
